package com.ruoyi.wms.service.impl;

import java.lang.reflect.Field;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.domain.QualityInspectionItem;
import com.ruoyi.wms.dto.QualityInspectionDto;
import com.ruoyi.wms.service.IQualityInspectionItemService;
import com.ruoyi.wms.vo.QualityInspectionVo;
import com.ruoyi.wms.vo.QualityInspectionVo2;
import com.ruoyi.wms.vo.QualityInspectionVo3;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.QualityInspectionMapper;
import com.ruoyi.wms.domain.QualityInspection;
import com.ruoyi.wms.service.IQualityInspectionService;

/**
 * 车辆质检Service业务层处理
 *
 * @author libancang
 * @date 2025-10-22
 */
@Service
public class QualityInspectionServiceImpl implements IQualityInspectionService {
    @Autowired
    private QualityInspectionMapper qualityInspectionMapper;


    @Autowired
    private IQualityInspectionItemService qualityInspectionItemService;

    /**
     * 查询车辆质检
     *
     * @param id 车辆质检主键
     * @return 车辆质检
     */
    @Override
    public QualityInspectionVo2 selectQualityInspectionById(String id) {
        QualityInspectionVo2 qualityInspectionVo2 = qualityInspectionMapper.selectQualityInspectionById(id);

        if (qualityInspectionVo2 == null) {
            return null;
        }

        QualityInspectionItem itemDto = new QualityInspectionItem();
        itemDto.setInspectionId(qualityInspectionVo2.getId());
        List<QualityInspectionItem> qualityInspectionItemList = qualityInspectionItemService.selectQualityInspectionItemList(itemDto);

        for (QualityInspectionItem item : qualityInspectionItemList) {
            String colName = item.getColName();
            if (StringUtils.isNotEmpty(colName)) {
                try {
                    // 直接根据colName获取字段
                    Field field = qualityInspectionVo2.getClass().getDeclaredField(colName);
                    field.setAccessible(true);
                    field.set(qualityInspectionVo2, item);
                } catch (NoSuchFieldException e) {
                    // 如果Vo2中没有对应的字段，跳过并记录日志
                    System.out.println("Vo2中未找到字段: " + colName);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        return qualityInspectionVo2;
    }

    /**
     * 查询车辆质检列表
     *
     * @param qualityInspection 车辆质检
     * @return 车辆质检
     */
    @Override
    public List<QualityInspectionVo> selectQualityInspectionList(QualityInspectionDto qualityInspection) {
        return qualityInspectionMapper.selectQualityInspectionList(qualityInspection);
    }

    /**
     * 新增车辆质检
     *
     * @param qualityInspection 车辆质检
     * @return 结果
     */
    @Override
    public int insertQualityInspection(QualityInspection qualityInspection) {

        qualityInspection.setId(MD5Utils.md5(qualityInspection.getPlateNo()));
        qualityInspection.setCreateUser(SecurityUtils.getUsername());
        qualityInspection.setCreateDate(DateUtils.getCurrentTime());
        qualityInspection.setUpdateDate(DateUtils.getCurrentTime());

        return qualityInspectionMapper.insertQualityInspection(qualityInspection);
    }

    /**
     * 修改车辆质检
     *
     * @param qualityInspection 车辆质检
     * @return 结果
     */
    @Override
    public int updateQualityInspection(QualityInspection qualityInspection) {
        int updateResult = 0;
        if (StringUtils.isEmpty(qualityInspection.getId())) {
            updateResult = insertQualityInspection(qualityInspection);
        }
        qualityInspection.setUpdateDate(DateUtils.getCurrentTime());
        updateResult = qualityInspectionMapper.updateQualityInspection(qualityInspection);
        if (0 == updateResult) {
            updateResult = insertQualityInspection(qualityInspection);
        }

        return updateResult;
    }

    /**
     * 批量删除车辆质检
     *
     * @param ids 需要删除的车辆质检主键
     * @return 结果
     */
    @Override
    public int deleteQualityInspectionByIds(String[] ids) {
        return qualityInspectionMapper.deleteQualityInspectionByIds(ids);
    }

    /**
     * 删除车辆质检信息
     *
     * @param id 车辆质检主键
     * @return 结果
     */
    @Override
    public int deleteQualityInspectionById(String id) {
        return qualityInspectionMapper.deleteQualityInspectionById(id);
    }

    @Override
    public int save(QualityInspectionVo3 vo2) {
        int saveCount = 0;
        // 获取所有字段
        Field[] fields = vo2.getClass().getDeclaredFields();

        for (Field field : fields) {
            String fieldName = field.getName();
            // 只处理以"Item"结尾的字段
            if (fieldName.endsWith("Item")) {
                try {
                    field.setAccessible(true);
                    QualityInspectionItem item = (QualityInspectionItem) field.get(vo2);
                    item.setColName(fieldName);
                    // 检查item不为空
                    if (item != null) {
                        if (StringUtils.isNotEmpty(item.getPlateNo())) {
                            qualityInspectionItemService.updateQualityInspectionItem(item);
                            saveCount++;
                        }

                    }
                } catch (IllegalAccessException e) {
                    // 处理异常，记录日志等
                    e.printStackTrace();
                }
            }
        }

        QualityInspection qualityInspection = new QualityInspection();
        BeanUtils.copyProperties(vo2, qualityInspection);
        updateQualityInspection(qualityInspection);
        return saveCount;
    }
}
